package com.wyu.blog.service;


import com.wyu.blog.dao.UserMapper;
import com.wyu.blog.dto.GitHubUser;
import com.wyu.blog.entity.User;
import com.wyu.blog.util.BlogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;


    public User createOrUpdateUser(GitHubUser gitHubUser) {
        String token = UUID.randomUUID().toString();
        User user = userMapper.selectUserByName(gitHubUser.getName() + "_" + gitHubUser.getId());
        //如果存在更新token及修改时间
        if(user != null){
//            if(gitHubUser.getName() == null || gitHubUser.getName().equals("")){
//                user.setUserName("github_" + gitHubUser.getId() + gitHubUser.getName());
//            }
//            else if(gitHubUser.getName() !=null && user.getUserName() !=null
//                    && !gitHubUser.getName().equals(user.getUserName())){
//                user.setUserName("git_" + gitHubUser.getName());
//            }
            if(!gitHubUser.getAvatarUrl().equals(user.getAvatarUrl())){
                user.setAvatarUrl(gitHubUser.getAvatarUrl());
            }
            user.setToken(token);
            userMapper.updateUserById(user);
            return user;
        }else{
            //如果不存在添加新的用户信息
            User newUser = new User();
//            if(StringUtils.isBlank(gitHubUser.getName())){
                newUser.setUserName(gitHubUser.getName() + "_" + gitHubUser.getId());
//            }else{
//                newUser.setUserName(gitHubUser.getName());
//            }
            String str = UUID.randomUUID().toString().substring(0, 6);
            newUser.setUserSalt(UUID.randomUUID().toString().substring(0, 5));
            newUser.setUserId(gitHubUser.getId());
            newUser.setUserPassword(BlogUtil.MD5(str + newUser.getUserSalt()));
            newUser.setToken(token);
            newUser.setAvatarUrl(gitHubUser.getAvatarUrl());
            newUser.setUserRegistrationTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            userMapper.insertGithubUser(newUser);
            return newUser;
        }
    }



    /**
     * 通过名字查询用户信息
     * @param name
     * @return
     */
    public User findUserByName(String name){
        return userMapper.selectUserByName(name);
    }

    /**
     * 查询所有用户信息
     * @return
     */
    public List<User> findAllUser(){
        return userMapper.selectAllUser();
    }


    /**
     * 添加用户
     * @param user
     * @return
     */
    public Integer addUser(User user) {
        return userMapper.insertUser(user);
    }

    /**
     * 后台添加用户
     * @param user
     * @return
     */
    public Integer addUserByBack(User user) {
        return userMapper.insertUserByBack(user);
    }


    /**
     * 删除用户
     * @param userId
     * @return
     */
    public int deleteUser(Integer userId) {
        return userMapper.deleteUserById(userId);
    }

    /**
     * 根据ID查询用户信息
     * @param userId
     * @return
     */
    public User findUserByUserId(Integer userId) {
        return userMapper.selectUserByUserId(userId);
    }

    /**
     * 根据ID修改用户信息
     * @param user
     * @return
     */
    public Integer updateUser(User user) {
        return userMapper.updateByUserId(user);
    }

    public Integer updateUserByUser(User user) {
        return userMapper.updateByUser(user);
    }

    public Integer updatePassword(User user) {
        return userMapper.updatePassword(user);
    }

    public Integer updatePhoto(User user) {
        return userMapper.updatePhoto(user);
    }

}
